import axios from 'axios'
import store from '@/store'
import { getToken } from '@/utils/auth'
const service = axios.create({
  baseURL: window.VUE_APP_BASE_URL,
  // 超时时间
  timeout: 1800000
})


// 请求拦截
service.interceptors.request.use(
  config => {
    /* if (localStorage.getItem('token')) {
      config.headers['sessionId'] = localStorage.getItem('token');
    } */
    /* console.log('123:::', store.getters.token, getToken()) */
    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['sessionId'] = getToken()
    }
    return config;
  },
  error => {
    console.log('request:::', error);
    return Promise.reject(error);
  }
)
// 响应拦截
service.interceptors.response.use(
  // 自定义 code 标识请求状态
  response => {
    const res = response.data
    // 登录态失效
    // if (res.indexOf('333') > -1) {
    //   store.commit('user/logout')
    // }
    return res
  },
  error => {
    console.error('response:::', error);
    return Promise.reject({
      msg: '网络错误',
      err: error
    })
  }
)
export default service
